package com.zhiyu.interceptor;

import com.zhiyu.entity.VO.ResponseVO;
import com.zhiyu.exception.EncryptException;
import com.zhiyu.exception.TokenException;
import com.zhiyu.util.ResponseUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    @Autowired
    private ResponseUtil responseUtil;

    @ExceptionHandler(EncryptException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public void handleEncryptException() {
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseVO handleMethodArgumentNotValidException() {
        return responseUtil.fieldInvalid();
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(AuthenticationException.class)
    public ResponseVO handleAuthenticationException() {
        return responseUtil.tokenInvalid();
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(TokenException.class)
    public ResponseVO handleTokenException() {
        return responseUtil.tokenInvalid();
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(UnauthorizedException.class)
    public ResponseVO handleUnauthorizedException() {
        return responseUtil.noPermission();
    }

    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(UnauthenticatedException.class)
    public ResponseVO handleUnauthenticatedException() {
        return responseUtil.requireAuthentication();
    }
}
